package org.bao.bestmatch;

import java.util.LinkedList;

import org.bao.bestmatch.model.Module;
import org.bao.bestmatch.model.TFT_CFPair;

/**
 * count yield rate(good product rate).
 * @author baoxiwu<bb@feijiao.info>,Cell&Module,BOE DT
 * @version 0.9.0
 * @since 2010-11-21
 * @license LGPL
 * 
 */
public class YieldCounter {
	//TODO calculation POLICY need T.B.C.
	//TODO POLICY NOT CORRECT.
	/*
	public static String count(LinkedList<TFT_CFPair> pair){
		int ng = 0;
		int cf_ng = 0;
		int tft_ng = 0;
		int max_ng = 0; 
		
		
		for(int i = 0; i < pair.size(); i++){
			cf_ng += Module.countNG(pair.get(i).getCF());
			tft_ng += Module.countNG(pair.get(i).getTFT());
			
			ng += pair.get(i).getNG();
		}
		
		max_ng = (cf_ng > tft_ng ? cf_ng : tft_ng);
		
		int sum = pair.getFirst().size() * pair.size();
		
		
		float rate = (float)(sum - ng) / (sum - max_ng);
		
		return String.format("%.1f", rate * 100);
	}
	
	*/
	
	
	
	/**
	 * 
	 * @param pair
	 * @return String[]{%TOTAL%, %CF_G_NUM%, %CF_RATE%, %TFT_G_NUM%, %TFT_RATE%, %CELL_G_NUM%, %CELL_RATE%, %G_RATE%}
	 */
	public static String[] count(LinkedList<TFT_CFPair> pair){
		/*
		 * String[]{%TOTAL%, %CF_G_NUM%, %CF_RATE%, %TFT_G_NUM%, %TFT_RATE%, %CELL_G_NUM%, %CELL_RATE%, %G_RATE%}
		 */
		int ng = 0;
		int cf_ng = 0;
		int tft_ng = 0;
		int max_ng = 0; 
		
		
		for(int i = 0; i < pair.size(); i++){
			cf_ng += Module.countNG(pair.get(i).getCF());
			tft_ng += Module.countNG(pair.get(i).getTFT());
			
			ng += pair.get(i).getNG();
		}
		
		max_ng = (cf_ng > tft_ng ? cf_ng : tft_ng);
		
		int sum = pair.getFirst().size() * pair.size();
		
		
		//float rate = (float)(sum - ng) / (sum - max_ng);
		
		
		String[] result = new String[8];
		
		result[0] = String.valueOf(sum);
		result[1] = String.valueOf(sum - cf_ng);
		
		result[2] = String.format("%.1f", 100 * (float)(sum - cf_ng) / sum);
		result[2] += "%";
		
		result[3] = String.valueOf(sum - tft_ng);
		
		result[4] = String.format("%.1f", 100* (float)(sum - tft_ng) / sum);
		result[4] += "%";
		
		result[5] = String.valueOf(sum - ng);
		
		result[6] = String.format("%.1f", 100 * (float)(sum - ng) / sum);
		result[6] += "%";
		
		result[7] = String.format("%.1f", 100 * (float)(sum - ng) / (sum - max_ng));
		result[7] += "%";
		
		return result;
	}
}
